package com.book.utils;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

import java.util.concurrent.TimeUnit;

/**
 * @param
 * @author oyz
 * @version 1.0.4
 * @description: TODO
 * @date 2023/11/6 21:48
 */
@Component
public class RedisUtils {

    @Autowired
    private RedisTemplate redisTemplate;


    public Object get(String key){
        if(null == key){
            throw new RuntimeException("the key is not exist");
        }
        return redisTemplate.opsForValue().get(key);
    }

    public void set(String key,Object value,long time){
        if(ObjectUtils.isEmpty(time)){
            redisTemplate.opsForValue().set(key,value);
        }else{
            redisTemplate.opsForValue().set(key,value, time,TimeUnit.SECONDS);
        }
    }

    public void remove(String key){
        redisTemplate.delete(key);
    }


}
